package com.example.myapplication.ui.Home.fragments;


import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.app.AppConstant;
import com.example.myapplication.base.BaseFragmentAdapter;
import com.example.myapplication.base.BaseMvpFragment;
import com.example.myapplication.model.bean.NewsChannelTable;
import com.example.myapplication.presenter.MainPresenter;
import com.example.myapplication.presenter.contract.MainContract;
import com.example.myapplication.ui.news.NewsChannelActivity;
import com.example.myapplication.ui.news.activity.NewsFrament;
import com.example.myapplication.utils.MyUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MainFragment extends BaseMvpFragment<MainPresenter> implements MainContract.View {
    private BaseFragmentAdapter adapter;
    @BindView(R.id.view_pager)
    ViewPager view_pager;
    @BindView(R.id.tabs)
    TabLayout tabs;

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    private int tabLayoutHeight;

    @BindView(R.id.add_channel_iv)
    ImageView mAdd;
    @BindView(R.id.fab)
    FloatingActionButton fab;

    @Override
    protected MainPresenter initPresenter() {
        return new MainPresenter(getActivity());
    }
    @Override
    protected int getLayoutId() {
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        return R.layout.fragment_main;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initEventAndData() {
        mPresenter.requestData();
    }


    @Override
    public void showError(String msg) {

    }

    @OnClick({R.id.add_channel_iv})
    public  void  addView(View view){
        NewsChannelActivity.startAction(getActivity());}

    private NewsFrament createListFragments(NewsChannelTable newsChannel) {
        NewsFrament fragment = new NewsFrament();
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.NEWS_ID, newsChannel.getNewsChannelId());
        bundle.putString(AppConstant.NEWS_TYPE, newsChannel.getNewsChannelType());
        bundle.putInt(AppConstant.CHANNEL_POSITION, newsChannel.getNewsChannelIndex());
        fragment.setArguments(bundle);
        return fragment;
    }



    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onDataSynEvent(String event) {
                mPresenter.requestData();
    }

    @Override
    public void Success(List<NewsChannelTable> newsChannelTables) {
        if (newsChannelTables != null) {
            List<String> channelNames = new ArrayList<>();
            List<Fragment> mNewsFragmentList = new ArrayList<>();
            for (int i = 0; i < newsChannelTables.size(); i++) {
                channelNames.add(newsChannelTables.get(i).getNewsChannelName());
                mNewsFragmentList.add(createListFragments(newsChannelTables.get(i)));
            }

            if (adapter == null) {
                adapter = new BaseFragmentAdapter(getChildFragmentManager(), mNewsFragmentList, channelNames);
            } else {
                //刷新fragment
                adapter.setFragments(getChildFragmentManager(), mNewsFragmentList, channelNames);
            }

            view_pager.setAdapter(adapter);
            tabs.setupWithViewPager(view_pager);
            tabLayoutHeight = tabs.getMeasuredHeight();
            tabs.measure(0, 0);
            MyUtils.dynamicSetTabLayoutMode(tabs);
        }
    }
}
